package com.ngkj.zmfptback.sys.service.read.impl;

import com.ngkj.zmfptCommon.common.util.TreeUtil;
import com.ngkj.zmfptback.sys.mapper.read.MenuReadMapper;
import com.ngkj.zmfptback.sys.service.read.MenuReadService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ChenHN
 * @since 2021/12/14 creation
 **/
@Service
public class MenuReadServiceImlp implements MenuReadService {

    @Resource
    private MenuReadMapper mapper;

    @Override
    public List<Map<String, Object>> getMenuTree() {
        List<Map<String, Object>> allMenus = mapper.getAllMenus();
        List<Map<String, Object>> tree = TreeUtil.getTree(allMenus, "id", "parentId");
        return tree;
    }

    @Override
    public List<Map<String, Object>> getMenuTree(List<String> roleList) {
        List<Map<String, Object>> menusByRoles = mapper.getMenusByRoles(roleList);
        List<Map<String, Object>> tree = TreeUtil.getTree(menusByRoles, "id", "parentId");
        return tree;
    }

    @Override
    public int getMaxOrderById(Integer id) {
        Integer maxOrderById = mapper.getMaxOrderById(id);
        if (ObjectUtils.isEmpty(maxOrderById)){
            return 10;
        }
        return (maxOrderById/10+1)*10;
    }
}
